package org.summerframework.component.security.core.util;

import org.springframework.data.redis.core.HashOperations;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 石超
 * @version v1.0.0
 */
public final class CIDUtil {

    private final static String CID_REDIS_KEY = "redis-CID-incr";

    /**
     * 创建cid
     *
     * @param hashOperations redisTemplate.opsForHash()
     * @param className      记录次数的key值, 继承CIDService的service的name   如: SceneService.class.getName()
     * @return 创建出来的cid
     */
    public static String createCIDName(HashOperations<String, String, Long> hashOperations, String className) {
        Long id = increment(hashOperations, className);
        Date date = new Date();

        SimpleDateFormat dayFormat = new SimpleDateFormat(String.format("yyyy-MM-dd%sHH-mm-ss", id));
        return dayFormat.format(date);
    }

    public static Long increment(HashOperations<String, String, Long> hashOperations, String className) {
        return hashOperations.increment(CID_REDIS_KEY, className, 1);
    }

    public static void reset(HashOperations<String, String, Long> hashOperations, String className) {
        hashOperations.put(CID_REDIS_KEY, className, 0L);
    }
}
